package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @GetMapping("/get")
    public Object get(String id){
        return userInfoService.get(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode){
        userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username, String password){
        Map map = userInfoService.login(username, password);
        return JsonResult.success(map);
    }

    @GetMapping("/currentUser")
    @RequireLogin
    public Object currentUser(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);
        return JsonResult.success(userInfo);
    }


    @GetMapping("/strategies/favor")
    public Object strategiesFavor(String userId){
        // 查询 用户对应的收藏 攻略id 集合
        List<String> sidList = userInfoRedisService.queryStrategyFavorListByUid(userId);
        return JsonResult.success(sidList);
    }

}
